home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Common / d3dFrame.cls < prev    next >
Text File  |  2001-10-08  |  33KB  |  1,017 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CD3DFrame"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14.  
  15. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  16. '
  17. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  18. '
  19. '  File:       D3DFrame.cls
  20. '  Content:    D3D Visual Basic Framework Frame object
  21. '
  22. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  23.  
  24.  
  25. Option Explicit
  26.  
  27. Public ObjectName As String        'Name of object
  28. Public Enabled As Boolean        'True to render 
  29.  
  30. Dim m_Matrix As D3DMATRIX        'local coordinate system matrix
  31. Dim m_MatConcat As D3DMATRIX        'world matrix coordinate system
  32. Dim m_MatConcatInv As D3DMATRIX        'inv world matrix 
  33.  
  34. Dim m_NumFrames As Long            'Number of child frame
  35. Dim m_NumMesh As Long            'Number of child mesh
  36. Dim m_MaxFrames As Long            'Max number of frames before reallocation
  37. Dim m_MaxMesh As Long            'Max number of meshes befor reallocation
  38. Dim m_DeletedFrames As Long        'Number of frame deleted
  39. Dim m_DeletedMesh As Long        'Number of mesh deleted
  40.  
  41. Dim m_D3DMesh() As CD3DMesh        'List of child Meshes
  42. Dim m_D3DFrame() As CD3DFrame        'List of Child Frame
  43.  
  44. Dim m_PreRenderEvent As DirectXEvent8    'Callback for prerender event
  45. Dim m_PreRenderId As Long        'Handle to send to prerender event
  46. Dim m_PostRenderEvent As DirectXEvent8    'Callback for postrender event
  47. Dim m_PostRenderId As Long        'Handle to send to postrender event
  48.  
  49. Dim m_SphereRadius As Single        'Bounding radius
  50. Dim m_SphereWorldPos As D3DVECTOR    'Bounding sphere center in world coord
  51. Dim m_SphereModelPos As D3DVECTOR    'Bounding sphere center in local coord
  52.  
  53. Dim m_pos As D3DVECTOR            'Position as set by SetPosition
  54. Dim m_quat As D3DQUATERNION        'Orientations as set by SetOrientation
  55. Dim m_rotcenter As D3DVECTOR        '
  56. Dim m_scale As Single            'Uniform scale as set by SetScale
  57.  
  58. Const kArrayStep = 5
  59.  
  60. Public Enum CombineTypeEnum
  61.     COMBINE_BEFORE = 0
  62.     COMBINE_AFTER = 1
  63.     COMBINE_REPLACE = 2
  64. End Enum
  65.  
  66. Public Enum TransformOp
  67.     TRANSFORM_COMPUTE = 0
  68.     TRANSFORM_FROMUPDATEFRAME = 1
  69.     TRANSFORM_DISABLE = 2
  70. End Enum
  71.  
  72.  
  73. '-----------------------------------------------------------------------------
  74. ' Name: SetPreRenderCallback
  75. ' Desc: callback to be called before Frame renders children
  76. '-----------------------------------------------------------------------------
  77. Public Function SetPreRenderCallback(cb As DirectXEvent8, id As Long)
  78.     Set m_PreRenderEvent = cb
  79.     m_PreRenderId = id
  80. End Function
  81.  
  82. '-----------------------------------------------------------------------------
  83. ' Name: SetPostRenderCallback
  84. ' Desc: callback to be called after Frame renders children
  85. '-----------------------------------------------------------------------------
  86. Public Function SetPostRenderCallback(cb As DirectXEvent8, id As Long)
  87.     Set m_PostRenderEvent = cb
  88.     m_PostRenderId = id
  89. End Function
  90.  
  91.  
  92.  
  93. '-----------------------------------------------------------------------------
  94. ' Name: AddChild
  95. ' Desc: adds another frame as a child 
  96. '-----------------------------------------------------------------------------
  97. Public Sub AddChild(child As CD3DFrame)
  98.     If m_MaxFrames = 0 Then
  99.         m_MaxFrames = kArrayStep
  100.         ReDim m_D3DFrame(m_MaxFrames)
  101.     ElseIf m_MaxFrames <= m_NumFrames Then
  102.         m_MaxFrames = m_MaxFrames + kArrayStep
  103.         ReDim Preserve m_D3DFrame(m_MaxFrames)
  104.     End If
  105.     Set m_D3DFrame(m_NumFrames) = child
  106.     m_NumFrames = m_NumFrames + 1
  107. End Sub
  108.  
  109. '-----------------------------------------------------------------------------
  110. ' Name: SetPosition
  111. ' Desc: sets position of the object in the parents coordinate system
  112. ' Note: will replace whatever matrix is set
  113. '-----------------------------------------------------------------------------
  114. Sub SetPosition(vec As D3DVECTOR)
  115.     m_pos = vec
  116.     UpdateMatrix
  117. End Sub
  118.  
  119. '-----------------------------------------------------------------------------
  120. ' Name: GetPosition
  121. ' Desc: returns the position of the object in the parents coordinate system
  122. '-----------------------------------------------------------------------------
  123. Function GetPosition() As D3DVECTOR
  124.     GetPosition.x = m_Matrix.m41
  125.     GetPosition.y = m_Matrix.m42
  126.     GetPosition.z = m_Matrix.m43
  127.  
  128. End Function
  129.  
  130. '-----------------------------------------------------------------------------
  131. ' Name: SetOrientation
  132. ' Desc: sets the orientation of the object
  133. ' Note: use D3DXQuaternionRotationAxis to create the quaternion
  134. ' Note: will replace whatever matrix is set
  135. '-----------------------------------------------------------------------------
  136. Sub SetOrientation(quat As D3DQUATERNION)
  137.     m_quat = quat
  138.     UpdateMatrix
  139. End Sub
  140.  
  141. '-----------------------------------------------------------------------------
  142. ' Name: GetOrientation
  143. ' Desc: returns what ever was passed to SetOrientation
  144. ' Note: does not inspect the matrix to obtain orientation
  145. '-----------------------------------------------------------------------------
  146. Function GetOrientation() As D3DQUATERNION
  147.     GetOrientation = m_quat
  148. End Function
  149.  
  150.  
  151. '-----------------------------------------------------------------------------
  152. ' Name: SetScale
  153. ' Desc: Sets the uniform scale of all children
  154. ' Note: will replace whatever matrix is set
  155. '-----------------------------------------------------------------------------
  156. Sub SetScale(val As Single)
  157.     m_scale = val
  158.     UpdateMatrix
  159. End Sub
  160.  
  161. '-----------------------------------------------------------------------------
  162. ' Name: GetScale
  163. ' Desc: returns what ever was passed to SetScale
  164. ' Note: does not inspect the matrix to obtain scale
  165. '-----------------------------------------------------------------------------
  166. Function GetScale() As Single
  167.     GetScale = m_scale
  168. End Function
  169.  
  170.  
  171.  
  172. '-----------------------------------------------------------------------------
  173. ' Name: SetOrientationCenter
  174. ' Desc: Sets the pivot point for the Frame
  175. ' Note: will replace whatever matrix is set
  176. '-----------------------------------------------------------------------------
  177. Sub SetOrientationCenter(vec As D3DVECTOR)
  178.     m_rotcenter = vec
  179.     UpdateMatrix
  180. End Sub
  181.  
  182. '-----------------------------------------------------------------------------
  183. ' Name: GetOrientationCenter
  184. ' Desc: returns whatever pivot point was set
  185. ' Note: does not inspect the matrix to obtain pivot
  186. '-----------------------------------------------------------------------------
  187. Function GetOrientationCenter() As D3DVECTOR
  188.     GetOrientationCenter = m_rotcenter
  189. End Function
  190.  
  191.  
  192. '-----------------------------------------------------------------------------
  193. ' Name: AddMesh
  194. ' Desc: Adds a Child Mesh object to the frame
  195. '-----------------------------------------------------------------------------
  196. Public Sub AddMesh(childMesh As CD3DMesh)
  197.     If m_MaxMesh = 0 Then
  198.         m_MaxMesh = kArrayStep
  199.         ReDim m_D3DMesh(m_MaxMesh)
  200.     ElseIf m_MaxMesh <= m_NumMesh Then
  201.         m_MaxMesh = m_MaxMesh + kArrayStep
  202.         ReDim Preserve m_D3DMesh(m_MaxMesh)
  203.     End If
  204.     Set m_D3DMesh(m_NumMesh) = childMesh
  205.     m_NumMesh = m_NumMesh + 1
  206.     
  207.     
  208. End Sub
  209.  
  210.  
  211.  
  212. '-----------------------------------------------------------------------------
  213. ' Name: AddD3DXMesh
  214. ' Desc: Adds a D3DX mesh object to the frame 
  215. ' Note: The d3dxmesh object is first encapuslated in a CD3DMesh object
  216. '-----------------------------------------------------------------------------
  217. Public Function AddD3DXMesh(mesh As D3DXMesh) As CD3DMesh
  218.     
  219.     Dim childMesh As CD3DMesh
  220.     Set childMesh = New CD3DMesh
  221.     childMesh.InitFromD3DXMesh mesh
  222.     
  223.     If m_MaxMesh = 0 Then
  224.         m_MaxMesh = kArrayStep
  225.         ReDim m_D3DMesh(m_MaxMesh)
  226.     ElseIf m_MaxMesh <= m_NumMesh Then
  227.         m_MaxMesh = m_MaxMesh + kArrayStep
  228.         ReDim Preserve m_D3DMesh(m_MaxMesh)
  229.     End If
  230.     Set m_D3DMesh(m_NumMesh) = childMesh
  231.     m_NumMesh = m_NumMesh + 1
  232.     
  233.     Set AddD3DXMesh = childMesh
  234. End Function
  235.  
  236. '-----------------------------------------------------------------------------
  237. ' Name: GetMatrix
  238. ' Desc: Returns the matrix for the local coordinate system
  239. '-----------------------------------------------------------------------------
  240. Public Function GetMatrix() As D3DMATRIX
  241.     GetMatrix = m_Matrix
  242. End Function
  243.  
  244. '-----------------------------------------------------------------------------
  245. ' Name: SetMatrix
  246. ' Desc: Sets the matrix for the local coordinate system
  247. ' Note: This overrides any previous calls to functions such as
  248. '       SetPosition, SetScale, SetOrientation
  249. '-----------------------------------------------------------------------------
  250. Public Function SetMatrix(m As D3DMATRIX)
  251.     m_Matrix = m
  252. End Function
  253.  
  254. '-----------------------------------------------------------------------------
  255. ' Name: GetChildMeshCount
  256. ' Desc: returns the number of child meshes
  257. '-----------------------------------------------------------------------------
  258. Public Function GetChildMeshCount()
  259.     GetChildMeshCount = m_NumMesh
  260. End Function
  261.  
  262.  
  263. '-----------------------------------------------------------------------------
  264. ' Name: GetChildMesh
  265. ' Desc: returns a given child mesh 
  266. '-----------------------------------------------------------------------------
  267. Public Function GetChildMesh(i As Long) As CD3DMesh
  268.     Set GetChildMesh = m_D3DMesh(i)
  269. End Function
  270.  
  271. '-----------------------------------------------------------------------------
  272. ' Name: GetChildFrame
  273. ' Desc: returns a given child frame
  274. '-----------------------------------------------------------------------------
  275. Public Function GetChildFrame(i As Long) As CD3DFrame
  276.     Set GetChildFrame = m_D3DFrame(i)
  277. End Function
  278.  
  279.  
  280. '-----------------------------------------------------------------------------
  281. ' Name: GetChildFrameCount
  282. ' Desc: returns number of Child Frames
  283. '-----------------------------------------------------------------------------
  284. Public Function GetChildFrameCount()
  285.     GetChildFrameCount = m_NumFrames
  286. End Function
  287.  
  288.  
  289.  
  290. '-----------------------------------------------------------------------------
  291. ' Name: FindChildObject
  292. ' Desc: Given an object name (flags are ignored) return the first child with
  293. '       a matching name. use the ClassName property to see if its a mesh
  294. '       or a frame object
  295. '-----------------------------------------------------------------------------
  296.  
  297. Public Function FindChildObject(Name As String, flags As Long) As Object
  298.     Dim i As Long
  299.     Dim aMesh As CD3DMesh
  300.     Dim aFrame As CD3DFrame
  301.     
  302.     'check self
  303.     If ObjectName = Name Then
  304.         Set FindChildObject = Me
  305.         Exit Function
  306.     End If
  307.  
  308.     
  309.     'check child mesh
  310.     For i = 0 To m_NumMesh - 1
  311.         Set aMesh = m_D3DMesh(i)
  312.         If Not aMesh Is Nothing Then
  313.             If aMesh.ObjectName = Name Then
  314.                 Set FindChildObject = aMesh
  315.                 Exit Function
  316.             End If
  317.         End If
  318.     Next
  319.     
  320.     Dim o As Object
  321.     'check child frames
  322.     For i = 0 To m_NumFrames - 1
  323.         Set aFrame = m_D3DFrame(i)
  324.         If Not aFrame Is Nothing Then
  325.             Set o = aFrame.FindChildObject(Name, flags)
  326.             If Not o Is Nothing Then
  327.                 Set FindChildObject = o
  328.                 Exit Function
  329.             End If
  330.         End If
  331.     Next
  332.     Set FindChildObject = Nothing
  333. End Function
  334.  
  335. '-----------------------------------------------------------------------------
  336. ' Name: FindChildParent
  337. ' Desc: given an object name find its parent frame
  338. '-----------------------------------------------------------------------------
  339.  
  340. Public Function FindChildParent(Name As String, flags As Long) As CD3DFrame
  341.     Dim i As Long
  342.     Dim aMesh As CD3DMesh
  343.     Dim aFrame As CD3DFrame
  344.     
  345.     'check child mesh
  346.     For i = 0 To m_NumMesh - 1
  347.         Set aMesh = m_D3DMesh(i)
  348.         If Not aMesh Is Nothing Then
  349.             If aMesh.ObjectName = Name Then
  350.                 Set FindChildParent = Me
  351.                 Exit Function
  352.             End If
  353.         End If
  354.     Next
  355.     
  356.     Dim o As Object
  357.     'check child frames
  358.     For i = 0 To m_NumFrames - 1
  359.         Set aFrame = m_D3DFrame(i)
  360.         If Not aFrame Is Nothing Then
  361.             If aFrame.ObjectName = Name Then
  362.                 FindChildParent = Me
  363.             End If
  364.             
  365.             Set o = aFrame.FindChildParent(Name, flags)
  366.             If Not o Is Nothing Then
  367.                 Set FindChildParent = o
  368.                 Exit Function
  369.             End If
  370.         End If
  371.     Next
  372.     
  373.     Set FindChildParent = Nothing
  374.     
  375. End Function
  376.  
  377.  
  378. '-----------------------------------------------------------------------------
  379. ' Name: Destroy
  380. ' Desc: Release all references
  381. '-----------------------------------------------------------------------------
  382. Public Sub Destroy()
  383.     ReDim m_D3DMesh(0)
  384.     ReDim m_D3DFrame(0)
  385.     m_NumFrames = 0
  386.     m_NumMesh = 0
  387. End Sub
  388.  
  389.  
  390.  
  391. '-----------------------------------------------------------------------------
  392. ' Name: InitFromXOF
  393. ' Desc: called from InitFromFile
  394. '-----------------------------------------------------------------------------
  395. Friend Sub InitFromXOF(dev As Direct3DDevice8, FileData As DirectXFileData, parent As CD3DFrame)
  396.     
  397.     Dim ChildData As DirectXFileData
  398.     Dim ChildObj As DirectXFileObject
  399.     Dim TypeGuid As String
  400.     Dim newmesh As CD3DMesh
  401.     Dim newFrame As CD3DFrame
  402.  
  403.     If Not parent Is Nothing Then parent.AddChild Me
  404.     
  405.     ObjectName = FileData.GetName()
  406.     
  407.     Do
  408.         'list of object - should return nothing at end of list
  409.         Set ChildData = FileData.GetNextObject()
  410.         If ChildData Is Nothing Then Exit Do
  411.     
  412.         Select Case ChildData.GetType()
  413.                
  414.             Case "TID_D3DRMFrame"
  415.                 Set newFrame = New CD3DFrame
  416.                 newFrame.InitFromXOF dev, ChildData, Me
  417.                 Set newFrame = Nothing
  418.   
  419.             Case "TID_D3DRMMesh"
  420.                 Set newmesh = New CD3DMesh
  421.                 newmesh.InitFromXOF dev, ChildData
  422.                 AddMesh newmesh
  423.                 Set newmesh = Nothing
  424.   
  425.             Case "TID_D3DRMFrameTransformMatrix"
  426.                 ChildData.GetDataFromOffset "", 0, Len(m_Matrix), m_Matrix
  427.             
  428.         End Select
  429.             
  430.     Loop
  431.     
  432. End Sub
  433.  
  434.  
  435. '-----------------------------------------------------------------------------
  436. ' Name: InitFromFile
  437. ' Desc: Called from D3DUtil_LoadFromFile
  438. '-----------------------------------------------------------------------------
  439. Public Function InitFromFile(dev As Direct3DDevice8, strFile As String, parent As CD3DFrame, animParent As CD3DAnimation) As Boolean
  440.  
  441.  
  442.     Dim xfile As DirectXFile
  443.     Dim enumX As DirectXFileEnum
  444.     Dim ChildData As DirectXFileData
  445.     Dim rootid As Long
  446.     Dim childid As Long
  447.     Dim strPath As String
  448.     Dim newmesh As CD3DMesh
  449.     Dim newFrame As CD3DFrame
  450.     
  451.     On Local Error GoTo errOut
  452.                 
  453.     
  454.     'create the X file object
  455.     Set xfile = g_dx.DirectXFileCreate()
  456.     
  457.     'make sure it understand MeshBuilders etc
  458.     xfile.RegisterDefaultTemplates
  459.     
  460.     On Local Error Resume Next
  461.     
  462.     'Open the file - will propegate file not found if fails
  463.     Set enumX = xfile.CreateEnumObject(strFile)
  464.     If Err.Number <> 0 Then
  465.     
  466.         On Local Error GoTo errOut
  467.         strPath = g_mediaPath + strFile
  468.         Set enumX = xfile.CreateEnumObject(strPath)
  469.     
  470.     End If
  471.     
  472.             
  473.     'Inspect each top level object in the file
  474.     Do
  475.         Set ChildData = enumX.GetNextDataObject()
  476.         If ChildData Is Nothing Then Exit Do
  477.                 
  478.         If ChildData.GetType = "TID_D3DRMFrame" Then
  479.             Set newFrame = New CD3DFrame
  480.             newFrame.InitFromXOF dev, ChildData, Me
  481.             Set newFrame = Nothing
  482.             
  483.         ElseIf ChildData.GetType = "TID_D3DRMMesh" Then
  484.             Set newmesh = New CD3DMesh
  485.             newmesh.InitFromXOF dev, ChildData
  486.             AddMesh newmesh
  487.             Set newmesh = Nothing
  488.             
  489.         ElseIf ChildData.GetType = "TID_D3DRMAnimationSet" Then
  490.             'TODO Parse Animation Set Data
  491.             If Not animParent Is Nothing Then
  492.                 animParent.ParseAnimSet ChildData, Me
  493.             End If
  494.         ElseIf ChildData.GetType = "TID_D3DRMMaterialSet" Then
  495.             'TODO Parse TopLevel Material Data
  496.             
  497.         End If
  498.     Loop
  499.  
  500.     If Not parent Is Nothing Then
  501.         parent.AddChild Me
  502.     End If
  503.                     
  504.     InitFromFile = True
  505.     Exit Function
  506.     
  507. errOut:
  508.     InitFromFile = False
  509.     
  510. End Function
  511.  
  512.  
  513.  
  514.  
  515.  
  516. '-----------------------------------------------------------------------------
  517. ' Name: SetFVF
  518. ' Desc: Changes the Flexible Vertex Format of all child objects
  519. '-----------------------------------------------------------------------------
  520. Public Sub SetFVF(dev As Direct3DDevice8, fvf As Long)
  521.     Dim aMesh As CD3DMesh
  522.     Dim aFrame As CD3DFrame
  523.     Dim i As Long
  524.     
  525.     For i = 0 To m_NumMesh - 1
  526.         Set aMesh = m_D3DMesh(i)
  527.         If Not aMesh Is Nothing Then
  528.             aMesh.SetFVF g_dev, fvf
  529.         End If
  530.     Next
  531.     
  532.     For i = 0 To m_NumFrames - 1
  533.         Set aFrame = m_D3DFrame(i)
  534.         If Not aFrame Is Nothing Then
  535.             aFrame.SetFVF g_dev, fvf
  536.         End If
  537.     Next
  538.  
  539. End Sub
  540.  
  541.  
  542. '-----------------------------------------------------------------------------
  543. ' Name: FlipNormals
  544. ' Desc: Flips the normals on all child objects (provided they are of D3DFVF_VERTEX)
  545. '-----------------------------------------------------------------------------
  546. Public Sub FlipNormals()
  547.     Dim aMesh As CD3DMesh
  548.     Dim aFrame As CD3DFrame
  549.     Dim i As Long
  550.     
  551.     For i = 0 To m_NumMesh - 1
  552.         Set aMesh = m_D3DMesh(i)
  553.         If Not aMesh Is Nothing Then
  554.             aMesh.FlipNormals
  555.         End If
  556.     Next
  557.     
  558.     For i = 0 To m_NumFrames - 1
  559.         Set aFrame = m_D3DFrame(i)
  560.         If Not aFrame Is Nothing Then
  561.             aFrame.FlipNormals
  562.         End If
  563.     Next
  564.  
  565. End Sub
  566.  
  567.  
  568. '-----------------------------------------------------------------------------
  569. ' Name: ComputeNormals
  570. ' Desc: Computes normals on all child objects (provided they have a normal set set)
  571. '-----------------------------------------------------------------------------
  572. Public Sub ComputeNormals()
  573.     Dim aMesh As CD3DMesh
  574.     Dim aFrame As CD3DFrame
  575.     Dim i As Long
  576.     
  577.     For i = 0 To m_NumMesh - 1
  578.         Set aMesh = m_D3DMesh(i)
  579.         If Not aMesh Is Nothing Then
  580.             aMesh.ComputeNormals
  581.         End If
  582.     Next
  583.     
  584.     For i = 0 To m_NumFrames - 1
  585.         Set aFrame = m_D3DFrame(i)
  586.         If Not aFrame Is Nothing Then
  587.             aFrame.ComputeNormals
  588.         End If
  589.     Next
  590.  
  591. End Sub
  592.  
  593.  
  594. '-----------------------------------------------------------------------------
  595. ' Name: Optimize
  596. ' Desc: 
  597. '-----------------------------------------------------------------------------
  598. Public Sub Optimize()
  599.     Dim aMesh As CD3DMesh
  600.     Dim aFrame As CD3DFrame
  601.     Dim i As Long
  602.     
  603.     For i = 0 To m_NumMesh - 1
  604.         Set aMesh = m_D3DMesh(i)
  605.         If Not aMesh Is Nothing Then
  606.             aMesh.Optimize
  607.         End If
  608.     Next
  609.     
  610.     For i = 0 To m_NumFrames - 1
  611.         Set aFrame = m_D3DFrame(i)
  612.         If Not aFrame Is Nothing Then
  613.             aFrame.Optimize
  614.         End If
  615.     Next
  616.  
  617. End Sub
  618.  
  619.  
  620.  
  621. '-----------------------------------------------------------------------------
  622. ' Name: ComputeBoundingVolumes
  623. ' Desc: ComputesBoundingVolumes for all child objects
  624. '-----------------------------------------------------------------------------
  625. Public Sub ComputeBoundingVolumes()
  626.         
  627.     Dim aMesh As CD3DMesh
  628.     Dim aFrame As CD3DFrame
  629.     Dim i As Long
  630.     
  631.     For i = 0 To m_NumMesh - 1
  632.         Set aMesh = m_D3DMesh(i)
  633.         If Not aMesh Is Nothing Then
  634.             aMesh.ComputeBoundingVolumes
  635.         End If
  636.     Next
  637.     
  638.     For i = 0 To m_NumFrames - 1
  639.         Set aFrame = m_D3DFrame(i)
  640.         If Not aFrame Is Nothing Then
  641.             aFrame.ComputeBoundingVolumes
  642.         End If
  643.     Next
  644.  
  645. End Sub
  646.  
  647.  
  648. '-----------------------------------------------------------------------------
  649. ' Name: UpdateFrames
  650. ' Desc: Called to precompute the WorldMatrices for each child object
  651. ' Note: Must be used prior to calling Mesh.RenderSkin
  652. '-----------------------------------------------------------------------------
  653. Public Sub UpdateFrames()
  654.     ComputeChildMatricesEx g_identityMatrix
  655. End Sub
  656.  
  657. '-----------------------------------------------------------------------------
  658. ' Name: GetUpdatedMatrix
  659. ' Desc: Returns the precomputed matrix
  660. '-----------------------------------------------------------------------------
  661. Public Function GetUpdatedMatrix() As D3DMATRIX
  662.     GetUpdatedMatrix = m_MatConcat
  663. End Function
  664.  
  665.  
  666. '-----------------------------------------------------------------------------
  667. ' Name: ComputeChildMatricesEx
  668. ' Desc: Aux function for UpdateFrames
  669. '-----------------------------------------------------------------------------
  670. Friend Sub ComputeChildMatricesEx(matSavedWorld As D3DMATRIX)
  671.     
  672.     
  673.     Dim aFrame As CD3DFrame
  674.     Dim i As Long
  675.     Dim det As Single
  676.     
  677.     Call D3DXMatrixMultiply(m_MatConcat, m_Matrix, matSavedWorld)
  678.     Call D3DXMatrixInverse(m_MatConcatInv, det, m_MatConcat)
  679.            
  680.     For i = 0 To m_NumFrames - 1
  681.         Set aFrame = m_D3DFrame(i)
  682.         If Not aFrame Is Nothing Then
  683.             aFrame.ComputeChildMatricesEx m_MatConcat
  684.         End If
  685.     Next
  686.  
  687. End Sub
  688.  
  689.  
  690.  
  691.  
  692.  
  693. '-----------------------------------------------------------------------------
  694. ' Name: RenderEx
  695. ' Desc: Render Child Objects
  696. ' Params:
  697. '    dev            Device to render to
  698. '    bDrawOpaqueSubsets    Render all objects with an material alpha of 1
  699. '    bDrawAlphaSubsets    Render all objects with transparent alpha 
  700. '    transform        
  701. '                TRANSFORM_COMPUT indicates to calculate world matrix
  702. '                TRANSFORM_FROMUPDATEFRAME inidcates to use precomputed matrix
  703. '                TRANSFORM_DISABLE does not set any world matrix 
  704. '                and uses what ever has been currently set    
  705. '-----------------------------------------------------------------------------
  706. Friend Sub RenderEx(dev As Direct3DDevice8, bDrawOpaqueSubsets As Boolean, bDrawAlphaSubsets As Boolean, transform As TransformOp)
  707.     
  708.     Dim matSavedWorld As D3DMATRIX
  709.     Dim matWorld As D3DMATRIX
  710.     Dim aFrame As CD3DFrame
  711.     Dim aMesh As CD3DMesh
  712.     Dim vCenter As D3DVECTOR
  713.     
  714.     If Not Enabled Then Exit Sub
  715.     
  716.     Dim i As Long
  717.     
  718.     
  719.     If transform = TRANSFORM_COMPUTE Then
  720.         Call g_dev.GetTransform(D3DTS_WORLD, matSavedWorld)
  721.         Call D3DXMatrixMultiply(matWorld, m_Matrix, matSavedWorld)
  722.         Call g_dev.SetTransform(D3DTS_WORLD, matWorld)
  723.     ElseIf transform = TRANSFORM_FROMUPDATEFRAME Then
  724.         Call g_dev.SetTransform(D3DTS_WORLD, m_MatConcat)
  725.     End If
  726.     
  727.     
  728.     If Not m_PreRenderEvent Is Nothing Then
  729.         m_PreRenderEvent.DXCallback m_PreRenderId
  730.     End If
  731.     
  732.     For i = 0 To m_NumMesh - 1
  733.         Set aMesh = m_D3DMesh(i)
  734.         If Not aMesh Is Nothing Then
  735.             
  736.             If g_bClipMesh And aMesh.bHasSphere Then
  737.                 D3DXVec3TransformCoord vCenter, aMesh.SphereCenter, matWorld
  738.                 If D3DUtil_IsSphereVisible(vCenter, aMesh.SphereRadius) <> 0 Then
  739.                     aMesh.RenderEx g_dev, bDrawOpaqueSubsets, bDrawAlphaSubsets
  740.                 Else
  741.                     'Debug.Print "cull " + aMesh.ObjectName
  742.                 End If
  743.             Else
  744.                 aMesh.RenderEx g_dev, bDrawOpaqueSubsets, bDrawAlphaSubsets
  745.             End If
  746.         End If
  747.     Next
  748.     
  749.     For i = 0 To m_NumFrames - 1
  750.         Set aFrame = m_D3DFrame(i)
  751.         If Not aFrame Is Nothing Then
  752.             aFrame.RenderEx g_dev, bDrawOpaqueSubsets, bDrawAlphaSubsets, transform
  753.         End If
  754.     Next
  755.         
  756.     If transform = TRANSFORM_COMPUTE Then
  757.         Call g_dev.SetTransform(D3DTS_WORLD, matSavedWorld)
  758.     End If
  759.     
  760.     If Not m_PostRenderEvent Is Nothing Then
  761.         m_PostRenderEvent.DXCallback m_PostRenderId
  762.     End If
  763.  
  764.  
  765. End Sub
  766.  
  767.  
  768.  
  769.  
  770. '-----------------------------------------------------------------------------
  771. ' Name: RenderSkins
  772. ' Desc: Searches the children for any mesh with skinned objects
  773. '       and renders them
  774. ' Note: BoneMatrice must be precalulated before rendering
  775. '       and bones must also be attached to appropriate mesh
  776. '-----------------------------------------------------------------------------
  777. Friend Sub RenderSkins()
  778.     Dim i As Long
  779.     Dim aMesh As CD3DMesh
  780.     Dim aFrame As CD3DFrame
  781.     
  782.     If Not m_PreRenderEvent Is Nothing Then
  783.         m_PreRenderEvent.DXCallback m_PreRenderId
  784.     End If
  785.         
  786.     For i = 0 To m_NumMesh - 1
  787.         Set aMesh = m_D3DMesh(i)
  788.         If Not aMesh Is Nothing Then
  789.             aMesh.RenderSkin
  790.         End If
  791.     Next
  792.     
  793.     For i = 0 To m_NumFrames - 1
  794.         Set aFrame = m_D3DFrame(i)
  795.         If Not aFrame Is Nothing Then
  796.             aFrame.RenderSkins
  797.         End If
  798.     Next
  799.     
  800.     If Not m_PostRenderEvent Is Nothing Then
  801.         m_PostRenderEvent.DXCallback m_PostRenderId
  802.     End If
  803.  
  804. End Sub
  805.  
  806. '-----------------------------------------------------------------------------
  807. ' Name: AttatchBonesToMesh
  808. ' Desc:
  809. '-----------------------------------------------------------------------------
  810. Friend Sub AttatchBonesToMesh(parent As CD3DFrame)
  811.     
  812.     Dim i As Long
  813.     Dim aMesh As CD3DMesh
  814.     Dim aFrame As CD3DFrame
  815.     
  816.         
  817.     For i = 0 To m_NumMesh - 1
  818.         Set aMesh = m_D3DMesh(i)
  819.         If Not aMesh Is Nothing Then
  820.             aMesh.AttatchBonesToMesh parent
  821.         End If
  822.     Next
  823.     
  824.     For i = 0 To m_NumFrames - 1
  825.         Set aFrame = m_D3DFrame(i)
  826.         If Not aFrame Is Nothing Then
  827.             aFrame.AttatchBonesToMesh parent
  828.         End If
  829.     Next
  830.     
  831. End Sub
  832.  
  833. '-----------------------------------------------------------------------------
  834. ' Name: Render
  835. ' Desc: render all child objects
  836. '-----------------------------------------------------------------------------
  837. Public Sub Render(dev As Direct3DDevice8)
  838.         
  839.     dev.SetTransform D3DTS_WORLD, g_identityMatrix
  840.         
  841.     'Render opaque subsets in  the meshes
  842.     RenderEx g_dev, True, False, TRANSFORM_COMPUTE
  843.         
  844.     
  845.     ' Enable alpha blending
  846.     Call dev.SetRenderState(D3DRS_ALPHABLENDENABLE, 1)
  847.     Call dev.SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA)
  848.     Call dev.SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)
  849.  
  850.     dev.SetTransform D3DTS_WORLD, g_identityMatrix
  851.     
  852.     RenderEx g_dev, False, True, TRANSFORM_COMPUTE
  853.         
  854.     Call dev.SetRenderState(D3DRS_ALPHABLENDENABLE, 0)
  855.     
  856. End Sub
  857.  
  858.  
  859. '-----------------------------------------------------------------------------
  860. ' Name: ClassName
  861. '-----------------------------------------------------------------------------
  862. Public Function ClassName() As String
  863.     ClassName = "CD3DFrame"
  864. End Function
  865.  
  866.  
  867. '-----------------------------------------------------------------------------
  868. ' Name: Class_Initialize()
  869. '-----------------------------------------------------------------------------
  870. Private Sub Class_Initialize()
  871.     D3DXMatrixIdentity m_Matrix
  872.     D3DXMatrixIdentity g_identityMatrix
  873.         
  874.     Call D3DXQuaternionIdentity(m_quat)
  875.     m_scale = 1
  876.     Enabled = True
  877.     
  878. End Sub
  879.  
  880. '-----------------------------------------------------------------------------
  881. ' Name: AddRotation
  882. ' Param:
  883. '    combinetype    PreMultiply/PostMultiply/or replace current matrix
  884. '    x,y,z          axis of rotation
  885. '    rot        radians to rotate
  886. '-----------------------------------------------------------------------------
  887. Public Function AddRotation(combinetype As CombineTypeEnum, x As Single, y As Single, z As Single, rot As Single)
  888.     
  889.     Dim mat As D3DMATRIX
  890.     D3DXMatrixRotationAxis mat, vec3(x, y, z), rot
  891.     
  892.     Select Case combinetype
  893.         Case COMBINE_BEFORE
  894.             D3DXMatrixMultiply m_Matrix, mat, m_Matrix
  895.         Case COMBINE_AFTER
  896.             D3DXMatrixMultiply m_Matrix, m_Matrix, mat
  897.         Case COMBINE_replace
  898.             m_Matrix = mat
  899.     End Select
  900.  
  901. End Function
  902.  
  903.  
  904. '-----------------------------------------------------------------------------
  905. ' Name: AddScale
  906. ' Param:
  907. '    combinetype    PreMultiply/PostMultiply/or replace current matrix
  908. '    x,y,z          scale
  909. '-----------------------------------------------------------------------------
  910. Public Function AddScale(combinetype As CombineTypeEnum, x As Single, y As Single, z As Single)
  911.     
  912.     Dim mat As D3DMATRIX
  913.     D3DXMatrixScaling mat, x, y, z
  914.     Select Case combinetype
  915.         Case COMBINE_BEFORE
  916.             D3DXMatrixMultiply m_Matrix, mat, m_Matrix
  917.         Case COMBINE_AFTER
  918.             D3DXMatrixMultiply m_Matrix, m_Matrix, mat
  919.         Case COMBINE_replace
  920.             m_Matrix = mat
  921.     End Select
  922.  
  923. End Function
  924.  
  925. '-----------------------------------------------------------------------------
  926. ' Name: AddTranslation
  927. ' Param:
  928. '    combinetype    PreMultiply/PostMultiply/or replace current matrix
  929. '    x,y,z          translation
  930. '-----------------------------------------------------------------------------
  931. Public Function AddTranslation(combinetype As CombineTypeEnum, x As Single, y As Single, z As Single)
  932.     
  933.     Dim mat As D3DMATRIX
  934.     D3DXMatrixTranslation mat, x, y, z
  935.     Select Case combinetype
  936.         Case COMBINE_BEFORE
  937.             D3DXMatrixMultiply m_Matrix, mat, m_Matrix
  938.         Case COMBINE_AFTER
  939.             D3DXMatrixMultiply m_Matrix, m_Matrix, mat
  940.         Case COMBINE_replace
  941.             m_Matrix = mat
  942.     End Select
  943.  
  944. End Function
  945.  
  946.  
  947. '-----------------------------------------------------------------------------
  948. ' Name: TransformCoord
  949. ' Desc: Transform a vector by the pre-computed matrix
  950. '-----------------------------------------------------------------------------
  951. Public Function TransformCoord(vIn As D3DVECTOR) As D3DVECTOR
  952.     Dim vOut As D3DVECTOR
  953.     D3DXVec3TransformCoord vOut, vIn, m_MatConcat
  954.     TransformCoord = vOut
  955. End Function
  956.  
  957. '-----------------------------------------------------------------------------
  958. ' Name: TransformNormal
  959. ' Desc: Transform a normal by the pre-computed matrix
  960. '-----------------------------------------------------------------------------
  961. Public Function TransformNormal(vIn As D3DVECTOR) As D3DVECTOR
  962.     Dim vOut As D3DVECTOR
  963.     D3DXVec3TransformNormal vOut, vIn, m_MatConcat
  964.     TransformNormal = vOut
  965. End Function
  966.  
  967.  
  968. '-----------------------------------------------------------------------------
  969. ' Name: InverseTransformCoord
  970. ' Desc: Transform a vector by the inverse of the pre-computed matrix
  971. '-----------------------------------------------------------------------------
  972. Public Function InverseTransformCoord(vIn As D3DVECTOR) As D3DVECTOR
  973.     Dim vOut As D3DVECTOR
  974.     D3DXVec3TransformCoord vOut, vIn, m_MatConcatInv
  975.     InverseTransformCoord = vOut
  976. End Function
  977.  
  978. '-----------------------------------------------------------------------------
  979. ' Name: InverseTransformNormal
  980. ' Desc: Transform a normal by the inverse of the pre-computed matrix
  981. '-----------------------------------------------------------------------------
  982. Public Function InverseTransformNormal(vIn As D3DVECTOR) As D3DVECTOR
  983.     Dim vOut As D3DVECTOR
  984.     D3DXVec3TransformNormal vOut, vIn, m_MatConcatInv
  985.     InverseTransformNormal = vOut
  986. End Function
  987.  
  988.  
  989. '-----------------------------------------------------------------------------
  990. ' Name: InvalidateDeviceObjects
  991. ' Desc:
  992. '-----------------------------------------------------------------------------
  993. Public Sub InvalidateDeviceObjects()
  994.     'all objects are managed so nothing to do
  995. End Sub
  996.  
  997.  
  998. '-----------------------------------------------------------------------------
  999. ' Name: RestoreDeviceObjects
  1000. ' Desc:
  1001. '-----------------------------------------------------------------------------
  1002. Public Sub RestoreDeviceObjects(dev As Direct3DDevice8)
  1003.     'all objects are managed so nothing to do
  1004. End Sub
  1005.  
  1006.  
  1007. '-----------------------------------------------------------------------------
  1008. ' Name: UpdateMatrix
  1009. ' Desc: Aux function for SetPosition, SetOrientation, SetScale
  1010. '-----------------------------------------------------------------------------
  1011. Private Sub UpdateMatrix()
  1012.     On Local Error Resume Next
  1013.     D3DXMatrixAffineTransformation m_Matrix, m_scale, m_rotcenter, m_quat, m_pos
  1014. End Sub
  1015.  
  1016.  
  1017.